﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WL_Store.BLL;
using WL_Store.Model;

namespace WL_Store.Web.subpage
{
    public partial class Products_List : System.Web.UI.Page
    {
        CommodityInfoBLL bll = new CommodityInfoBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetList();
                gv_bind();
                lbl_CountProducts.Text = bll.CountBlurbs().ToString();
            }
            
        }
        /// <summary>
        /// 给repater绑定数据
        /// </summary>
        public void gv_bind() {
            //获取检索的信息
            string Id = txt_search_Id.Text.Trim();
            string name = txt_search_name.Text.Trim();
            int caId = Convert.ToInt32(ddl_category.SelectedValue);
            DateTime? StartAddtime = null;
            if (!string.IsNullOrWhiteSpace(txt_StartAddtime.Value))
            {
                StartAddtime = Convert.ToDateTime(txt_StartAddtime.Value.Trim());
            }
            DateTime? EndAddtime = null;
            if (!string.IsNullOrWhiteSpace(txt_EndAddtime.Value))
            {
                EndAddtime = Convert.ToDateTime(txt_EndAddtime.Value.Trim());
            }


            //类的初始化器
            CommodityInfoSearchModel model = new CommodityInfoSearchModel() {
                Id = Id,
                name=name,
                categoryId=caId,
                StartAddtime=StartAddtime,
                EndAddtime=EndAddtime
            };

            int totalCount;

            List<CommodityInfoModel> list = bll.GetCommodityInfosProc(model,product_Pager.CurrentPageIndex,product_Pager.PageSize,out totalCount);
            //为分页组件的总计录进行赋值
            product_Pager.RecordCount = totalCount;

            //DataTable list = bll.GetCommodityInfos();
            this.rpt_product.DataSource = list;
            this.rpt_product.DataBind();

        }
        protected void btn_add_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                //获取商品信息
                string number = this.txt_number.Text.Trim();
                string name = this.txt_nam.Text.Trim();
                string purchase = this.txt_purchase.Text.Trim();
                string selling = this.txt_selling.Text.Trim();
                string productionPlace = this.txt_productionPlace.Text.Trim();
                string stocks = this.txt_stocks.Text.Trim();
                string quantitySold = this.txt_quantitySold.Text.Trim();
                string description = this.txt_description.Text.Trim();
                int CaId = Convert.ToInt32(this.ddl_Add.SelectedValue);
                //实例化业务逻辑层
                CommodityInfoModel model = new CommodityInfoModel();
                
                model.commodityID = Convert.ToInt32(number);
                model.comName = name;
                model.purchase = purchase;
                model.selling = selling;
                model.productionPlace = productionPlace;
                model.stocks = Convert.ToInt32(stocks);
                model.quantitySold = Convert.ToInt32(quantitySold);
                model.description = description;
                model.addTime = DateTime.Now;
                model.categoryId = CaId;
                if (bll.AddCommodityInfosProc(model))
                {
                    //Response.Write("<script>alert('添加成功');location.href='Products_List.aspx'</script>");
                    Page.ClientScript.RegisterStartupScript(this.GetType(),"alert", "<script> parent.layer.msg('添加成功！', { time: 1000, shade: 0.3 },function(){layer.close(layer.open());ClearText();});</script></script>");
                    gv_bind();
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('添加失败！');</script>");
                    gv_bind();
                }
            }
        }

        protected void btn_clear_Click(object sender, EventArgs e)
        {

        }

        protected void lbt_del_Command(object sender, CommandEventArgs e)
        {
            //判断是否是点击删除命令
            if (e.CommandName == "del")
            {
                int commodityID = Convert.ToInt32(e.CommandArgument);
                if (bll.DeleteCommodityInfoProc(commodityID))
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>parent.layer.msg('删除成功！', { time: 1000, shade: 0.3 });</script>");
                    gv_bind();
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>parent.layer.msg('删除失败！', { time: 1000, shade: 0.3 });</script>");
                   
                }
            }
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
                gv_bind();
            
        }

        protected void product_Pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            //为分页组件的当前页索引赋值
            product_Pager.CurrentPageIndex = e.NewPageIndex;
            //重新绑定数据
            gv_bind();
        }


        private void GetList()
        {
            DataTable dt = bll.GetCategory();

            ddl_category.DataSource = dt;
            ddl_category.DataTextField = "category";
            ddl_category.DataValueField = "categoryId";
            ddl_category.DataBind();

            ddl_category.Items.Insert(0,new ListItem("全部","0"));

            ddl_Add.DataSource = dt;
            ddl_Add.DataTextField = "category";
            ddl_Add.DataValueField = "categoryId";
            ddl_Add.DataBind();

        }

    }
}